package com.lindenvalley.ch.content;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

import com.lindenvalley.ch.AbstractHomeActivity;

@Root(name="week")
public class Week implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Attribute (name="week_num")
	private byte index;
	
	@Attribute (name="custom_week_num")
	private int customWeekNumber;
	
	@Attribute (name="start_week")
	private long startDate;
	
	@Attribute (name="end_week")
	private long endDate;
	
	@ElementList(inline=true, name="task", required=false)
	private List<Task> tasks;
	
	private final String dateFormatPattern = "dd.MM.yyyy";
	private final String dateFormatPatternShort = "dd.MM";
	private transient SimpleDateFormat mDateFormat;
	private transient SimpleDateFormat mDateFormatShort;
	private transient Calendar mCalendar;

	public Week() {
		super();
		init();
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		String weekOfYear = "";
		mCalendar.setTimeInMillis(getStartDate() * 1000L);
		weekOfYear = String.valueOf(AbstractHomeActivity.getWeekOfYear(mCalendar));
		
		String dateText = getLongStartDate() + " - " + getLongEndDate();
		String ausbildText = AbstractHomeActivity.sAusbildungsnachweis+getRealWeekNumber();
		String text = "Kalenderwoche " + weekOfYear +"\n" + dateText + ausbildText;
		return text;
	}

	private void init(){
		mDateFormat = new SimpleDateFormat(dateFormatPattern);
		mDateFormatShort = new SimpleDateFormat(dateFormatPatternShort);
		mCalendar = Calendar.getInstance();
	}

	/**
	 * @return the index
	 */
	public byte getIndex() {
		return index;
	}

	/**
	 * @param index
	 *            the index to set
	 */
	public void setIndex(byte index) {
		this.index = index;
	}

	/**
	 * @return the startDate
	 */
	public long getStartDate() {
		return startDate;
	}
	
	/**
	 * @return the short startDate
	 */
	public String getShortStartDate() {
		if (mDateFormatShort == null)
			init();
		mCalendar.setTimeInMillis(startDate * 1000L);
		return mDateFormatShort.format(mCalendar.getTime());
	}
	
	/**
	 * @return the short startDate
	 */
	public String getLongStartDate() {
		if (mDateFormat == null)
			init();
		mCalendar.setTimeInMillis(startDate * 1000L);
		return mDateFormat.format(mCalendar.getTime());
	}

	/**
	 * @param startDate
	 *            the startDate to set
	 */
	public void setStartDate(long startDate) {
		this.startDate = startDate;
	}

	/**
	 * @return the endDate
	 */
	public long getEndDate() {
		return endDate;
	}
	
	/**
	 * @return the short end date
	 */
	public String getShortEndDate() {
		if (mDateFormatShort == null)
			init();
		mCalendar.setTimeInMillis((endDate - 7200L) * 1000L);
		return mDateFormatShort.format(mCalendar.getTime());
	}
	
	/**
	 * @return the long end date
	 */
	public String getLongEndDate() {
		if (mDateFormat == null)
			init();
		mCalendar.setTimeInMillis((endDate - 7200L) * 1000L);
		return mDateFormat.format(mCalendar.getTime());
	}

	/**
	 * @param endDate
	 *            the endDate to set
	 */
	public void setEndDate(long endDate) {
		this.endDate = endDate;
	}

	/**
	 * @return the tasks
	 */
	public List<Task> getTasks() {
		return tasks;
	}
	
	public List<Task> getTaskForDay(int day){
		List<Task> dayTask = null;
		if (tasks != null){
			dayTask = new ArrayList<Task>();
			if (mCalendar == null){
				init();
			}
			for (Task task: tasks){
					mCalendar.setTimeInMillis(task.getDate() * 1000L);
					if (mCalendar.get(Calendar.DAY_OF_WEEK) == day){
						dayTask.add(task);
					}
			}
		}
		return dayTask;
	}

	/**
	 * @param tasks
	 *            the tasks to set
	 */
	public void setTasks(List<Task> tasks) {
		this.tasks = tasks;
	}

	public void addTask(Task task) {
		if (this.tasks == null){
			this.tasks = new ArrayList<Task>();
		}
		int index = tasks.indexOf(task);
		if (index == -1){
			this.tasks.add(task);
		} else {
			this.tasks.set(index, task);
		}
	}

	public boolean isEmpty() {
		return tasks == null || tasks.isEmpty() ? true : false;
	}
	
	public Task getTaskById(int id){
		Task task = null;
		if (tasks != null && tasks.size() > 0){
			for (Task t: tasks){
				if (t.getTaskId() == id){
					task = t;
					break;
				}			
			}
		}
		return task;
	}

	public boolean isContainsDate(long date){
		boolean result = false;
		if (startDate != 0 && endDate != 0){
			if (date >= startDate && date <= endDate){
				result = true;
			}
		}
		return result;
	}

	public int getCustomWeekNumber() {
		return customWeekNumber;
	}

	public void setCustomWeekNumber(int customWeekNumber) {
		this.customWeekNumber = customWeekNumber;
	}
	
	public int getRealWeekNumber(){
		return (customWeekNumber == 0 ? index : customWeekNumber); 
	}

}
